import qs from "qs";
import baseUrl from "./path.js"

export const myRequest = (options,li) => {
	let requestUrl = baseUrl.wy;
	if(li) {
		requestUrl = baseUrl.lb;
	}
	const requestData = formatParams(options.data);
	return new Promise((resolve, reject) => {
		if (!options.text) {
			uni.showLoading({
				title: '加载中'
			});
		} 
		uni.request({
			url: requestUrl + options.url,
			header: {
				'Content-Type': 'application/json'
			},
			method: options.method || 'GET',
			data: requestData,
			success: (res) => {
				uni.hideLoading();
				const {
					data,
					message,
					status
				} = res.data;
			
				if (status == 200 || status == 200) {
					if(Array.isArray(data)) {
						if(data.length === 0) {
							resolve(true);
						} else {
							resolve(data);
						}
					} else {
						resolve(data);
					}
					
					
				} else if (status == 401) { //跳登录
					var pages = getCurrentPages();
					const route = pages[pages.length - 1].$page
				    //token失效
					console.log(route);
					let url = route.path;
					let id = route.options.id;
				    console.log('条首页接口',options.url);
					// console.log(url);
					// console.log(id);
					if(id) {
						uni.navigateTo({
							url: '/pages/login/login?url='+ url +"?id=" + id
						});
					} else {
						uni.navigateTo({
							url: '/pages/login/login?url='+ url
						});
					}
		            
		             
						
				} else {
					uni.showToast({
						icon: 'none',
						title: message
					})
					return
				}
			},
			fail: (err) => {
				uni.hideLoading();
				console.log(err)
				reject(err)
			}
		})
	})
}

function formatParams(params) {
	// const token = uni.getStorageSync("token") ? uni.getStorageSync("token") : "";
	// if (!params) {
	// const params = {
	// 		token: token
	// 	}
	// } else {
	// 	params.token = token;
	// }
	//return qs.stringify({param: params})
	return params
}
